<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="应用唯一id" prop="appUniqueId">
      <el-input v-model="dataForm.appUniqueId" placeholder="应用唯一id"></el-input>
    </el-form-item>
    <el-form-item label="应用Id" prop="appId">
      <el-input v-model="dataForm.appId" placeholder="应用Id"></el-input>
    </el-form-item>
    <el-form-item label="告警类型 1.调用减少 2.调用停止 3.调用故障 4.调用延时" prop="appType">
      <el-input v-model="dataForm.appType" placeholder="告警类型 1.调用减少 2.调用停止 3.调用故障 4.调用延时"></el-input>
    </el-form-item>
    <el-form-item label="单号" prop="transactionId">
      <el-input v-model="dataForm.transactionId" placeholder="单号"></el-input>
    </el-form-item>
    <el-form-item label="算法id" prop="arithId">
      <el-input v-model="dataForm.arithId" placeholder="算法id"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="appStatus">
      <el-input v-model="dataForm.appStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="错误代码" prop="appWarnCode">
      <el-input v-model="dataForm.appWarnCode" placeholder="错误代码"></el-input>
    </el-form-item>
    <el-form-item label="日志详情" prop="logMessage">
      <el-input v-model="dataForm.logMessage" placeholder="日志详情"></el-input>
    </el-form-item>
    <el-form-item label="日志生成的时间" prop="logDate">
      <el-input v-model="dataForm.logDate" placeholder="日志生成的时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          appUniqueId: '',
          appId: '',
          appType: '',
          transactionId: '',
          arithId: '',
          appStatus: '',
          appWarnCode: '',
          logMessage: '',
          logDate: '',
          createDate: ''
        },
        dataRule: {
          appUniqueId: [
            { required: true, message: '应用唯一id不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '应用Id不能为空', trigger: 'blur' }
          ],
          appType: [
            { required: true, message: '告警类型 1.调用减少 2.调用停止 3.调用故障 4.调用延时不能为空', trigger: 'blur' }
          ],
          transactionId: [
            { required: true, message: '单号不能为空', trigger: 'blur' }
          ],
          arithId: [
            { required: true, message: '算法id不能为空', trigger: 'blur' }
          ],
          appStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          appWarnCode: [
            { required: true, message: '错误代码不能为空', trigger: 'blur' }
          ],
          logMessage: [
            { required: true, message: '日志详情不能为空', trigger: 'blur' }
          ],
          logDate: [
            { required: true, message: '日志生成的时间不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subappwarnlog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.appUniqueId = data.subAppWarnLog.appUniqueId
                this.dataForm.appId = data.subAppWarnLog.appId
                this.dataForm.appType = data.subAppWarnLog.appType
                this.dataForm.transactionId = data.subAppWarnLog.transactionId
                this.dataForm.arithId = data.subAppWarnLog.arithId
                this.dataForm.appStatus = data.subAppWarnLog.appStatus
                this.dataForm.appWarnCode = data.subAppWarnLog.appWarnCode
                this.dataForm.logMessage = data.subAppWarnLog.logMessage
                this.dataForm.logDate = data.subAppWarnLog.logDate
                this.dataForm.createDate = data.subAppWarnLog.createDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subappwarnlog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'appUniqueId': this.dataForm.appUniqueId,
                'appId': this.dataForm.appId,
                'appType': this.dataForm.appType,
                'transactionId': this.dataForm.transactionId,
                'arithId': this.dataForm.arithId,
                'appStatus': this.dataForm.appStatus,
                'appWarnCode': this.dataForm.appWarnCode,
                'logMessage': this.dataForm.logMessage,
                'logDate': this.dataForm.logDate,
                'createDate': this.dataForm.createDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
